﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinHeWechat.EF;
using XinHeWechat.Model;

namespace XinHeWechat.Server
{
    public class HouseOwnerIdentityServer
    {
        #region 单例模式
        private static readonly HouseOwnerIdentityServer instance = null;
        static HouseOwnerIdentityServer()
        {
            instance = new HouseOwnerIdentityServer();
        }
        private HouseOwnerIdentityServer()
        {
        }
        public static HouseOwnerIdentityServer Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion

        /// <summary>
        /// 微信身份认证
        /// </summary>
        /// <param name="reqData"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Identity(IdentityRequest reqData, out string msg)
        {
            msg = null;

            using (XinHeDBEntities db = new XinHeDBEntities())
            {
                if (db.THouseOwnerIdentity.Any(p => p.OpenID == reqData.OpenID))
                {
                    msg = "您已验证通过!";
                    return false;
                }

                var houseOwnerEntity = db.THouseOwner.SingleOrDefault(p => p.Name == reqData.Name.Trim() && p.IDCard == reqData.IDCard && p.Phone == reqData.Phone);

                if (houseOwnerEntity == null)
                {
                    msg = "验证失败!请检查输入信息";
                    return false;
                }
                else
                {
                    var identityEntity = new THouseOwnerIdentity
                    {
                        ID = Guid.NewGuid(),
                        OpenID = reqData.OpenID,
                        HOID = houseOwnerEntity.HOID,
                        IdentityTime = DateTime.Now
                    };
                    db.THouseOwnerIdentity.Add(identityEntity);
                    db.SaveChanges();
                    return true;
                }
            }
        }
    }
}
